--[[
	登录模块
]]

local BaseFrame = appdf.req(appdf.CLIENT_SRC.."plaza.models.BaseFrame")
local protocol = require("gamemodel.protocol")
local LogonFrame = class("LogonFrame",BaseFrame)

local MultiPlatform = appdf.req(appdf.EXTERNAL_SRC .. "MultiPlatform")
local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")
local logincmd = appdf.req(appdf.HEADER_SRC .. "CMD_LogonServer")
local SDKUtils = appdf.req(appdf.EXTERNAL_SRC .. "SDKUtils")
local utils = appdf.req(appdf.EXTERNAL_SRC .. "Utils")
local Logon_ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "Logon_ExternalFun")
function LogonFrame:ctor(view,callback)
	LogonFrame.super.ctor(self,view,callback)
	self._plazaVersion = appdf.VersionValue(6,7,0,1)
	self._stationID = yl.STATION_ID
	local targetPlatform = cc.Application:getInstance():getTargetPlatform()
	local tmp = yl.DEVICE_TYPE_LIST[targetPlatform]
	self._deviceType = tmp or yl.DEVICE_TYPE
	self._szMachine = MultiPlatform:getInstance():getMachineId()
end

--网络信息
function LogonFrame:onSocketEvent(wMsgID,pData)
	-- print("onSocketEvent>>>>",wMsgID,pData)
	if wMsgID == protocol.SM_LOGIN_ACK then --登录命令
		-- self:onSubLogonEvent(pData)
		a=1
	elseif wMsgID == protocol.SM_USER_TOKEN then 
		GlobalUserItem.mark = pData.mark 
		cc.UserDefault:getInstance():setStringForKey("mark",pData.mark or 0)
	elseif wMsgID == protocol.SM_PLAYERINFO then 
		GlobalUserItem.onLoadData(pData)
	elseif wMsgID == protocol.SM_PLAYERINFOEX and not pData.subid then 
		GlobalUserItem.onUpdateData(pData)
	elseif wMsgID == protocol.SM_GAMELIST then --房间列表

		local roomlist = pData.gamels or {}
		table.sort(roomlist,function(a,b)
			return a.orderid < b.orderid
		end)

		yl.needRefreshGameList = Logon_ExternalFun.isChangeGameList( GlobalUserItem.roomkindlist , roomlist )
		GlobalUserItem.roomkindlist = roomlist
		GlobalUserItem.roomwithGameidlist = {}
		for k,v in pairs(GlobalUserItem.roomkindlist) do
			GlobalUserItem.roomwithGameidlist[v.gameid] = v
		end

	elseif wMsgID == protocol.SM_TABLEDATA then
		for i,room in pairs(GlobalUserItem.roomkindlist) do
			if room.gameid == pData.gameid then
				if room.roomls then
					for j,table in pairs(room.roomls) do
						if table.tableid then
							if table.tableid == pData.tableid and table.srvid == pData.srvid  and table.roomid == pData.roomid  then
								room.roomls[j] = pData
								return
							end
						else
							if table.roomid == pData.roomid then
								room.roomls[j] = pData
								return
							end
						end
					end
				end
				table.insert( room.roomls, pData )
			end
		end
	elseif wMsgID == protocol.SM_DELTABLE then

			for i,room in pairs(GlobalUserItem.roomkindlist) do
				if room.gameid == pData.gameid then
					if room.roomls then
						for j,table in pairs(room.roomls) do
							if table.tableid then
								if table.tableid == pData.tableid and table.srvid == pData.srvid  and table.roomid == pData.roomid  then
									table.remove( room.roomls, j )
								end
							else
								if table.roomid == pData.roomid then
									table.remove( room.roomls, j )
									return
								end
							end
						end
					end
				end
			
			end
	end

	if nil ~= self._callBack then
		self._callBack(wMsgID,pData)
	end		
end

--获取邮件
function LogonFrame:sendSocketData(wctrol,wmsgid,jsonstr)

	self:sendData(wctrol,wmsgid, jsonstr)

end

-- function LogonFrame:sendVisitor()


-- 	-- local js = {msgid=protocol.CM_LOGIN,loginmode=protocol.VISITOR_MODE,
-- 	-- machineid=appdf.MachineCode,channelid=appdf.channelId}
-- 	-- local jsonstr = cjson.encode(js)
-- 	-- self:sendData(protocol.CTRL_CLIENT_HALL,protocol.CM_LOGIN, jsonstr)
-- 	self:sendVisitor()

-- end
--获取游戏公告
function LogonFrame:sendGetNOtice()

	local js = {msgid=protocol.CXM_GETNOTICE}
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_GETNOTICE, jsonstr)

end

--获取邮件
function LogonFrame:sendGetMyMail(idx)

	local js = {msgid=protocol.CXM_GETMAIL,idx=idx}
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_GETMAIL, jsonstr)

end

--获取客服消息
function LogonFrame:sendKefuMsg(idx)
	
	local js = {msgid=protocol.CXM_GETREPLY,idx=idx}
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_GETREPLY, jsonstr)
	
end
--提交客服消息
function LogonFrame:sendSubToKefu(content)
	local js = {msgid=protocol.CXM_FEEDBACK,content=content}
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_FEEDBACK, jsonstr)
	
end

--保险箱存入
function LogonFrame:sendBankSave(content)
	local js = {msgid=protocol.CXM_INSURE_IN,content=content}
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_INSURE_IN, jsonstr)
	
end


--保险箱取出
function LogonFrame:sendBankTake(content)
	local js = {msgid=protocol.CXM_INSURE_OUT,content=content}
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_INSURE_OUT, jsonstr)
	
end

--获取验证码
function LogonFrame:sendVerifCode(phone)
	local js = {msgid=protocol.CXM_VERIFYCODE,phone = phone,channelid = appdf.channelId, userid = GlobalUserItem.dwUserID}
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_VERIFYCODE, jsonstr)
	
end

--注册
function LogonFrame:sendRegister(username,verifycode,loginpwd,codeidx)
	local js = {msgid=protocol.CXM_REGISTER,username = username,verifycode = verifycode,userid = GlobalUserItem.dwUserID,loginpwd = loginpwd,channelid = appdf.channelId,vidx = tonumber(codeidx) }
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_REGISTER, jsonstr)
	
end

--保险箱存入
function LogonFrame:sendSaveBank(gold)
	local js = {msgid=protocol.CXM_INSUREGOLD_INC,gold = gold}
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_INSUREGOLD_INC, jsonstr)
	
end

--保险箱取出
function LogonFrame:sendTakeBank(gold)
	local js = {msgid=protocol.CXM_INSUREGOLD_DEC,gold = gold}
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_INSUREGOLD_DEC, jsonstr)
	
end

--退出游戏
function LogonFrame:sendLogonOut()
	self:sendData(protocol.CTRL_CLIENT_HALL,protocol.CM_LOGOUT)
	
end

--修改密码
function LogonFrame:sendChangePwd()
	local js = {msgid=protocol.CM_LOGOUT}
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_HALL,protocol.CM_LOGOUT, jsonstr)
	
end

--获取充值配置信息
function LogonFrame:sendGetPayInfo()
	local js = {msgid=protocol.CXM_GETRECHARGECFG}
	local jsonstr = cjson.encode(js)
	self:sendData(protocol.CTRL_CLIENT_HALL,protocol.CXM_GETRECHARGECFG, jsonstr)
	
end

local function checkMsg(msg)
	if not msg or type(msg) ~= 'table' then
		return false
	end
	return true
end

--绑定支付宝/银行卡
function LogonFrame:sendBindInform(msg)
	if not checkMsg(msg) then
		return 
	end
	local js = {msgid = protocol.CXM_EXCHANGE,exchangetype=msg.type,verifycode=msg.verifycode,gold=msg.gold * appdf.bili,vidx=tonumber(msg.vidx) ,VerifyCode = msg.VerifyCode }
	print("sssssssss",cjson.encode(js))
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_EXCHANGE,cjson.encode(js))
end

--查询兑换时尚圈
function LogonFrame:sendExchangeRecord(idx)
	local js = {msgid = protocol.CXM_EXCHANGERECORD,idx = idx}
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_EXCHANGERECORD,cjson.encode(js))
end

--更换头像
function LogonFrame:sendChangeFaceId(msg)
	local js = {msgid = protocol.CXM_CHANGEFACE,faceid=msg.faceid}
	print("-------------------changefaceid:",msg.faceid)
	self:sendData(protocol.CTRL_CLIENT_BRICK,protocol.CXM_CHANGEFACE,cjson.encode(js))
end

--继续游戏
function LogonFrame:sendContinueGame()
	self:sendData(protocol.CTRL_CLIENT_HALL,protocol.CM_GAMECONTINUE)
end



return LogonFrame